#import "BWColorKeyWindowController.h"
#import "BWGraphDocument.h"
#import "BWSymbolCell.h"
#import "BWColorKey.h"
#import "BWNumbTableHeaderCell.h"
#import "BWNumbTableHeaderView.h"

static BWColorKeyWindowController *g_controller;

@implementation BWColorKeyWindowController

- (void) setDocument: (NSDocument *) document
{
    [super setDocument: document];
    
    [self willChangeValueForKey: @"colorKey"];
    colorKey = [document valueForKey: @"colorKey"];
    [self didChangeValueForKey: @"colorKey"];

    if (document == nil) {
        [[self window] setTitle: @"Color Key Inspector"];
    }

} // setDocument


- (NSString *) windowTitleForDocumentDisplayName: (NSString *) displayName
{
    NSString *string;

    string = [NSString stringWithFormat: @"Color Key for %@", displayName];
    return (string);

} // windowTitleForDocumentDisplayName



+ (NSString *) autosaveName
{
    return (@"colorKeyWindow");
} // autosaveName


+ (BWWindowController *) sharedInstance
{
    return (g_controller);
} // sharedInstance


+ (void) setSharedInstance: (BWWindowController *) controller
{
    assert ([[controller class] isSubclassOfClass: self]);

    g_controller = (BWColorKeyWindowController *)controller;

    [(NSPanel*)[g_controller window] setBecomesKeyOnlyIfNeeded: YES];

} // setShraedInstance


+ (NSString *) windowNibName
{
    return (@"BWColorKeyWindow");
} // windowNibName


- (void) windowDidLoad
{
    [super windowDidLoad];

    NSTableColumn *column;
    column = [colorKeyTableView tableColumnWithIdentifier: @"symbolColumn"];
    [column setDataCell: [[BWSymbolCell alloc] init]];

    NSArray *columns = [colorKeyTableView tableColumns];

    NSEnumerator *enumerator = [columns objectEnumerator];

    while (column = [enumerator nextObject]) {
        NSView *originalHeaderView = [colorKeyTableView headerView];

        BWNumbTableHeaderView *view;
        view = [[BWNumbTableHeaderView alloc] 
                   initWithFrame: [originalHeaderView frame]];

        [colorKeyTableView setHeaderView: view];
        [view setTableView: colorKeyTableView];

        NSTableHeaderCell *originalCell;
        originalCell = [column headerCell];

        BWNumbTableHeaderCell *cell;
        
        cell = [[BWNumbTableHeaderCell alloc]
                   initTextCell: [originalCell title]];
        [cell setTitle: [originalCell title]];

        [column setHeaderCell: cell];
        [cell release];

    }

} // windowDidLoad


- (BWThread *) selectedThread
{
    BWThread *thread = nil;

    int row = [colorKeyTableView selectedRow];

    if (row != -1) {
        thread = [colorKey colorAtIndex: row];
    }

    return (thread);

} // selectedThread


+ (BWColorKeyWindowController *) sharedController
{
    return ((BWColorKeyWindowController*)[super sharedController]);
} // sharedController

@end // BWColorKeyWindowController

